import { KcpdMap } from "@/kcpd/maps";
import { Player } from "@minecraft/server";
import { KcqdMainState } from "./mainState";
import { KcqdContext, KcqdModule } from "./module";
import { AutoStopState } from "@sapi-game/gameState/common/autoStop";
import { PlayerGroupSet } from "@sapi-game/gamePlayer/groupSet";
import { KcpdFuncs } from "@/kcpd/func";
import { KcpdWatingState } from "@/kcpd/watingState";

export class kcqdGame extends KcqdModule.Engine<{
    players: Player[];
    map: KcpdMap;
}> {
    protected override buildContext(config: {
        players: Player[];
        map: KcpdMap;
    }): KcqdContext {
        return new KcqdContext(this.groupBuilder.fromPlayers(config.players), {
            roundTime: 40,
            map: config.map,
        });
    }

    override onStart(): void {
        this.pushState(KcpdWatingState, {
            players: this.context.players,
            next: () => {
                this.pushState(KcqdMainState);
                this.pushState(AutoStopState, {
                    groupSet: new PlayerGroupSet([this.context.players]),
                    onLeave: (p) => {
                        this.context.players.sendMessage(
                            `${p.name} 离开了烤肠抢夺赛`
                        );
                    },
                });
            },
        });
    }

    override onStop(): void {
        const winners = this.context.players
            .filter((p) => p.win)
            .map((p) => p.name);
        this.context.players.sendMessage(
            "游戏结束,获胜者:" + winners.join(",")
        );
        this.context.players.forEach((p) => {
            KcpdFuncs.hub(p.player);
        });
    }
}
